package blog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import blog.entity.Article;
import blog.entity.Category;
import blog.entity.Tag;
import blog.mapper.ArticleMapper;
import blog.service.ArticleService;

//在这里加service注解是为了引入ArticleMapper
@Service
public class ArticleServiceImpl implements ArticleService{
	@Resource
	private ArticleMapper articleMapper;
	
	public List<Article> listRecentArticle(Integer n) {
		return articleMapper.listRecentArticle(n);
	}

	
	public PageInfo<Article> getPageArticleList(Integer pageIndex, Integer pageSize) {
		
		//进行分页
		PageHelper.startPage(pageIndex, pageSize);
		
		List <Article> articleList = articleMapper.findAll();
		
		for(Article a:articleList) {
			//查出每个文章的分类信息。。。。。。。。。。。。。。
		}
		
		//包装查出来的列表
		return new PageInfo<Article> (articleList);
	}


	
	public void addArticle(Article article) {
		//往文章表中添数据
		articleMapper.addArticle(article);
		
		//往文章分类表中添数据
		List<Category>categoryList=article.getCategoryList();
		for(Category c: categoryList) {
			articleMapper.addArticleCategory(article.getArticleId(),c.getCategoryId());
		}
		
		//往文章标签表中添数据
		List<Tag> tagList=article.getTagList();
		for(Tag t:tagList) {
			articleMapper.addArticleTag(article.getArticleId(),t.getTagId());
		}
	}
}